package com.opmsys.mapper;

import com.opmsys.pojo.entity.Record;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RecordMapper {

    /**
     * 新增运行记录
     * @param record
     */
    @Insert("INSERT INTO t_recording (record_date, record_status, daily_production, current_pressure, record_details, person_number, pump_id) " +
            "VALUES (#{record.recordDate}, #{record.recordStatus}, #{record.dailyProduction}, #{record.currentPressure}, #{record.recordDetails}, #{record.personNumber}, #{record.pumpId})")
    void insertRecord(@Param("record")Record record);

    /**
     * 删除运行记录
     * @param id
     */
    @Delete("DELETE FROM t_recording WHERE id = #{id}")
    void deleteRecord(@Param("id") Integer id);

    /**
     * 根据id更新运行记录信息
     * @param record
     */
    @Update("<script>" +
                   "UPDATE t_recording " +
                   "<set>" +
                   "<if test='record.recordDate != null and record.recordDate != \"\" '> record_date = #{record.recordDate}, </if>" +
                   "<if test='record.recordStatus != null'> record_status = #{record.recordStatus}, </if>" +
                   "<if test='record.dailyProduction != null'> daily_production = #{record.dailyProduction}, </if>" +
                   "<if test='record.currentPressure != null'> current_pressure = #{record.currentPressure}, </if>" +
                   "<if test='record.recordDetails != null and record.recordDetails != \"\" '> record_details = #{record.recordDetails}, </if>" +
                   "<if test='record.personNumber != null'> person_number = #{record.personNumber}, </if>" +
                   "<if test='record.pumpId != null'> pump_id = #{record.pumpId}, </if>" +
                   "</set> " +
                   "<where>" +
                   "id = #{record.id} " +
                   "</where> " +
                   "</script>")
    void updateRecord(@Param("record")Record record);

    /**
     * 根据条件分页查询
     * @param recordDate
     * @param recordStatus
     * @param dailyProduction
     * @param currentPressure
     * @param recordDetails
     * @param personNumber
     * @param pumpId
     * @param limit
     * @param offset
     * @return
     */
    @Select("<script>" +
            "SELECT * FROM t_recording " +
            "<where>" +
            "<if test='recordDate != null'> AND record_date like concat('%',#{recordDate},'%') </if>" +
            "<if test='recordStatus != null'> AND record_status = #{recordStatus} </if>" +
            "<if test='currentPressure != null'> AND current_pressure = #{currentPressure}  </if>" +
            "<if test='recordDetails != null'> AND  record_details like concat('%',#{recordDetails},'%')  </if>" +
            "<if test='personNumber != null'> person_number = #{personNumber}  </if>" +
            "<if test='pumpId != null'> AND pump_id = #{pumpId}  </if>" +
            "</where> " +
            "ORDER BY id " +
            "LIMIT #{limit} OFFSET #{offset}"+
            "</script>")
    List<Record> getRecordPageWithConditions(@Param("recordDate") String recordDate,
                                         @Param("recordStatus") Integer recordStatus,
                                         @Param("dailyProduction") Float dailyProduction,
                                         @Param("currentPressure") Float currentPressure,
                                         @Param("recordDetails") String recordDetails,
                                         @Param("personNumber") Long personNumber,
                                         @Param("pumpId") Integer pumpId,
                                         @Param("limit") Integer limit,
                                         @Param("offset") Integer offset);

    /**
     * 条件查询不分页
     * @param recordDate
     * @param recordStatus
     * @param dailyProduction
     * @param currentPressure
     * @param recordDetails
     * @param personNumber
     * @param pumpId
     * @return
     */
    @Select("<script>" +
            "SELECT * FROM t_recording " +
            "<where>" +
            "<if test='recordDate != null'> AND record_date like concat('%',#{recordDate},'%') </if>" +
            "<if test='recordStatus != null'> AND record_status = #{recordStatus} </if>" +
            "<if test='currentPressure != null'> AND current_pressure = #{currentPressure}  </if>" +
            "<if test='recordDetails != null'> AND  record_details like concat('%',#{recordDetails},'%')  </if>" +
            "<if test='personNumber != null'> person_number = #{personNumber}  </if>" +
            "<if test='pumpId != null'> AND pump_id = #{pumpId}  </if>" +
            "</where> " +
            "ORDER BY id " +
            "</script>")
    List<Record> getRecordListWithConditions(@Param("recordDate") String recordDate,
                                           @Param("recordStatus") Integer recordStatus,
                                           @Param("dailyProduction") Float dailyProduction,
                                           @Param("currentPressure") Float currentPressure,
                                           @Param("recordDetails") String recordDetails,
                                           @Param("personNumber") Long personNumber,
                                           @Param("pumpId") Integer pumpId);

    /**
     * 查询指定日期运行记录
     * @param date
     * @return
     */
    @Select("SELECT * FROM t_recording WHERE record_date = #{date}")
    List<Record> ifdate(@Param("date") String date);

    @Select("select * from t_recording where pump_id = #{id}")
    List<Record> selectByPumpingId(Integer id);
}
